package com.own.component.common.util.function;

import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * LambdaFunction
 *
 * @author chenxueli
 * @date 2020/9/30 9:42
 */
public final class LambdaFunction {

    /**
     * 根据某个属性去重
     *
     * @param keyExtractor 键
     * @param <T>          类型
     * @return 去重后的对象
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        var seen = new ConcurrentHashMap<Object, Boolean>(32);
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

}
